package Chapter06;

/**
 * 非同步的数据读写
 * 多线程中的非同步问题主要体现在对域的读写上,如果让域自身避免这个问题,则就不需要
 * 修改操作该域的方法,有3中域自身就可以避免非同步的问题,即final域,有锁保护的域和volatile域
 * Created by a on 2017/6/11.
 */
public class Test132 {
    public static void main(String[] args) {
        Bank bank = new Bank();
        Thread thread1 = new Thread(new Transfer(bank));
        thread1.start();
        bank.deposit(100);
    }

}

class Bank {
    private volatile int account =100;
    public  void deposit(int money){
        account+=money;
    }
    public int getAccount(){
        return account;
    }
}

class Transfer implements Runnable {
    private Bank bank;

    public Transfer(Bank bank) {
        this.bank = bank;
    }

    public void run(){
        for (int i =0;i < 10;i++){
            bank.deposit(10);
            System.out.print("账户余额是:"+bank.getAccount()+"\n");
        }
    }
}